package com.xuecheng.content.api;

import com.xuecheng.execption.XueChengPlusException;
import com.xuecheng.model.dto.BindTeachplanMediaDto;
import com.xuecheng.model.dto.SaveTeachplanDto;
import com.xuecheng.model.dto.TeachplanDto;
import com.xuecheng.model.po.TeachplanMedia;
import com.xuecheng.service.TeachplanMediaService;
import com.xuecheng.service.TeachplanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 小班哥
 * @version 1.0
 * @date 2023/3/18 11:03
 * @description 课程计划编辑接口
 */

@Api(value = "课程计划编辑接口" , tags = "课程计划编辑接口")
@RestController
@Slf4j
public class TeachplanController {

    @Autowired
    private TeachplanService teachplanService;

    @Autowired
    private TeachplanMediaService teachplanMediaService;


    @ApiOperation("课程查询计划")
    @ApiImplicitParam(value = "courseId" , name = "课程ID" , required = true ,dataType = "long" , paramType = "path")
    @GetMapping("/teachplan/{courseId}/tree-nodes")
    public List<TeachplanDto> getTreeNodes(@PathVariable long courseId){
        return teachplanService.findTeachplayTree(courseId);
    }


    @ApiOperation("课程计划章节，添加，修改，保存")
    @PostMapping("/teachplan")
    public void saveTeachplan(@RequestBody SaveTeachplanDto dto){
        teachplanService.saveTeachplan(dto);
    }

    @ApiOperation("删除课程章节")
    @DeleteMapping("/teachplan/{careerID}")
    public void DeleteTeachplan(@PathVariable long careerID){
        teachplanService.DeletePrivilegeNotHeld(careerID);
    }


    @ApiOperation("课程章节排序往前")
    @PostMapping("/teachplan/moveup/{arentID}")
    public void OrderUP(@PathVariable long arentID){
        teachplanService.PullUP(arentID);
    }


    @ApiOperation("课程章节排序往后")
    @PostMapping("/teachplan/movedown/{arentID}")
    public void OrderDown(@PathVariable long arentID){
        teachplanService.PullDown(arentID);
    }


    @ApiOperation(value = "绑定媒资信息接口")
    @PostMapping("/teachplan/association/media")
    public void associationMedia(@RequestBody @Validated BindTeachplanMediaDto bindTeachplanMediaDto){
        TeachplanMedia teachplanMedia = teachplanMediaService.associationMedia(bindTeachplanMediaDto);
        if (teachplanMedia == null){
            throw new XueChengPlusException("绑定关联视频失败,请重试");
        }
    }

    @ApiOperation(value = "删除课程关联视频接口")
    @DeleteMapping("/teachplan/association/media/{teachplanID}/{MediaID}")
    public void DeleteAssociatedVideo(@PathVariable(value = "teachplanID") long courseID , @PathVariable(value = "MediaID") String MediaID){
        Boolean aBoolean = teachplanMediaService.DeleteCourseVideoMessage(courseID, MediaID);
        if (!aBoolean) {
            throw new XueChengPlusException("删除关联视频失败,请重试");
        }
    }


}
